import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lovify/models/enums/gender.dart';
import 'package:lovify/routes/app.dart';
import 'package:lovify/state/user_controller.dart';
import 'package:lovify/widgets/aq_avatar.dart';
import 'package:lovify/widgets/aq_button.dart';
import 'package:lovify/widgets/aq_cake.dart';
import 'package:lovify/widgets/aq_gender_switch.dart';
import 'package:lovify/widgets/aq_input.dart';
import 'package:lovify/widgets/aq_scroll_body.dart';
import 'package:lovify/widgets/aq_text.dart';

class LeaderScreen extends StatefulWidget {
  const LeaderScreen({super.key});

  @override
  State<LeaderScreen> createState() => _LeaderScreenState();
}

class _LeaderScreenState extends State<LeaderScreen> {
  final _userState = Get.find<UserController>();
  bool _isLoading = false;

  void _goHome() {
    Navigator.pushNamed(context as dynamic, AppRoutes.home);
  }

  Future<void> _doAction() async {
    setState(() {
      _isLoading = true;
    });

    bool status = await _userState.saved();

    setState(() {
      _isLoading = false;
    });

    if (status) {
      _goHome();
    }
  }

  Future<void> _init() async {
    setState(() {
      _isLoading = true;
    });
    bool status = await _userState.initialize();
    if (status) {
      _goHome();
    }
    setState(() {
      _isLoading = false;
    });
  }

  @override
  void initState() {
    super.initState();
    _init();
  }

  @override
  Widget build(BuildContext context) {
    return AQScrollBody(
      isLoading: _isLoading,
      children: [
        Obx(() {
          return AqAvatar(
            radius: 48,
            imageUrl: _userState.getAvatar(),
            onTap: _userState.uploadAvatar,
          );
        }),
        const SizedBox(height: 12),
        const AQText(
          fontSize: 10,
          content: "点击上传头像",
          color: Color(0xFF6B7280),
        ),
        const SizedBox(height: 12),
        const AQText(
          content: "欢迎来到爱确",
          fontSize: 24,
        ),
        const SizedBox(height: 24),
        AQInput(
          label: "姓名",
          placeholder: "请填写你的姓名",
          prefixIcon: Icons.person,
          onChanged: (value, _) => {_userState.setName(value)},
        ),
        const SizedBox(height: 12),
        AQCake(
          label: "生日",
          placeholder: "请选择你的生日",
          prefixIcon: Icons.cake,
          suffixIcon: Icons.calendar_today,
          onChanged: (value) => {_userState.setBirthday(value)},
        ),
        const SizedBox(height: 24),
        AqGenderSwitch(
          onChanged: (value) => {_userState.setGender(valueOfGender(value))},
        ),
        const SizedBox(height: 24),
        AQButton(
          label: "开始使用",
          onPressed: () {
            _doAction();
          },
        ),
      ],
    );
  }
}
